package com.xfapp.demos.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xfapp.demos.common.Result;
import com.xfapp.demos.entity.Question;
import com.xfapp.demos.service.IQuestionService;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 存储消防操作员刷题相关的题目 前端控制器
 * </p>
 *
 * @author jinYi
 * @since 2025-06-05
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Resource
    private IQuestionService questionService;
    @GetMapping("/findOfId/{id}")
    public Result findAll(@PathVariable String id)
    {
        List<Question> chapterList = questionService.list(new QueryWrapper<Question>().eq("chapter_id", id).eq("del_flag", 0));
        if (chapterList.size() == 0){
            return Result.error("-1","没有数据,请联系管理员");
        }
        return Result.success(chapterList);
    }

    @GetMapping("/findAll")
    public Result findAllList(){
        List<Question> chapterList = questionService.list(new QueryWrapper<Question>().eq("del_flag", 0));
        return Result.success(chapterList);
    }

    /**
     * 随机刷题
     * @param num
     * @return
     */
    @GetMapping("/randomFind/{num}")
    public Result findOfChapterId(@PathVariable(required = false)  Integer num) {
        int defaultNum = 30; // 默认值
        if (num == null || num <= 30) {
            num = defaultNum;
        }
        if (num == 200) {
            List<Question> singleChoice = questionService.list(new QueryWrapper<Question>()
                    .eq("del_flag", 0)
                    .eq("type", 1)); // 假设单选题类型为1

            List<Question> multipleChoice = questionService.list(new QueryWrapper<Question>()
                    .eq("del_flag", 0)
                    .eq("type", 2)); // 假设多选题类型为2

            List<Question> trueFalse = questionService.list(new QueryWrapper<Question>()
                    .eq("del_flag", 0)
                    .eq("type", 3)); // 假设判断题类型为3

            Collections.shuffle(singleChoice);
            Collections.shuffle(multipleChoice);
            Collections.shuffle(trueFalse);

            List<Question> selectedQuestions = new ArrayList<>();
            selectedQuestions.addAll(singleChoice.subList(0, Math.min(100, singleChoice.size())));
            selectedQuestions.addAll(multipleChoice.subList(0, Math.min(40, multipleChoice.size())));
            selectedQuestions.addAll(trueFalse.subList(0, Math.min(60, trueFalse.size())));

            if (selectedQuestions.isEmpty()) {
                return Result.error("-1", "没有符合要求的数据，请联系管理员");
            }

            return Result.success(selectedQuestions);
        }
        List<Question> allQuestions = questionService.list(new QueryWrapper<Question>().eq("del_flag", 0));


        if (allQuestions.isEmpty()) {
            return Result.error("-1", "没有数据，请联系管理员");
        }
        Collections.shuffle(allQuestions); // 打乱顺序

        List<Question> randomQuestions = allQuestions.subList(0, Math.min(num, allQuestions.size())); // 获取前30条

        return Result.success(randomQuestions);
    }
    @GetMapping("/randomFind")
    public Result findOfChapterIdDefault() {
        return findOfChapterId(null); // 复用原方法
    }
//    模拟考试数据
    @GetMapping("/findMockTest")
    public Result findOfChapterId() {

        return findOfChapterId(200);
    }
}
